<?php
/**
 * Created by PhpStorm.
 * User: leixiaotian
 * Date: 2020/9/22
 * Time: 11:17
 */

namespace App\Model;

use Illuminate\Support\Facades\DB;

class RsRelationshipModel extends BaseModel
{
    protected $connection = 'api';
    protected $table = "rs_relationship";
    protected $tablePrefix = '';

    protected $fields = array(
        'id',
        'user_id as userId',
        'friend_id as friendId',
        'type',
        'rs_code as rsCode',
        'intimate'
    );

    public function __construct(array $attributes = [])
    {
        $this->tablePrefix = env('DB_PREFIX_API');
    }

    public function getList( $offset=0, $row=10, $type = 3,$userId=0) {
        $table = $this->tablePrefix . $this->table;
        $condition = ' and type =' . $type;
        if ($userId != 0) {
            $condition = $condition . ' and user_id = ' . $userId;
        }
        $sql = "select rs_code as rsCode from {$table} where is_deleted = 0 {$condition} group by `rs_code` limit {$offset}, {$row}";
        $re = DB::connection($this->connection)->select($sql);
        $rowRs = self::objToArray($re);
        return $rowRs;
    }

    public function getCount($type = 3, $userId=0) {
        $table = $this->tablePrefix . $this->table;
        $condition = ' and type =' . $type;
        if ($userId != 0) {
            $condition = $condition . ' and user_id = ' . $userId;
        }
        $sql = "select count(*) as cou from (select rs_code as cou from {$table} where is_deleted = 0 {$condition} group by `rs_code`) as a";
        $re = DB::connection($this->connection)->select($sql);
        return $re[0]->cou;
    }
}